package trafficsimulator;

public class Map
{

    private char[][] map;
    private int modulo = 5;
    private int scale = 10;

    public Map()
    {
        int x = (int) Math.random() * scale;
        int y = (int) Math.random() * scale;

        while (x % modulo != 0)
        {
            x = (int) Math.random() * scale;
        }

        while (y % modulo != 0)
        {
            y = (int) Math.random() * scale;
        }
        map = new char[x][y];

        for (int i = 0; i != x; i++)
        {
            for (int j = 0; j != y; j++)
            {
                if (i % modulo == 0 || j % modulo == 0)
                {
                    map[i][j] = '1';
                } else
                {
                    map[i][j] = '0';
                }
            }
        }
    }

    public Map(int xSize, int ySize, int modulo)
    {
        this.modulo = modulo;
        int x = xSize;
        int y = ySize;
        map = new char[ySize][xSize];
        for (int i = 0; i != x; i++)
        {
            for (int j = 0; j != y; j++)
            {
                if (i % modulo == 0 || j % modulo == 0)
                {
                    map[j][i] = 'O';
                } else if (i == xSize - 1 || j == ySize - 1)
                {
                    map[j][i] = 'O';
                } else
                {
                    map[j][i] = '*';
                }
            }
        }
    }

    public char[][] getMap()
    {
        return this.map;
    }

    public int getMapXSize()
    {
        return map[0].length;
    }

    public int getMapYSize()
    {
        return map.length;
    }

    public void setCar(Car c)
    {
        map[c.getPreviousY()][c.getPreviousX()] = '1';
        map[c.getY()][c.getX()] = c.getDesignator();
    }

    public char getNext(char direction, int x, int y)
    {
        char retVal = 'I';
        if (direction == 'N')
        {
            retVal = map[y - 1][x];
        }
        if (direction == 'S')
        {
            retVal = map[y + 1][x];
        }
        if (direction == 'E')
        {
            retVal = map[y][x + 1];
        }
        if (direction == 'W')
        {
            retVal = map[y][x - 1];
        }
        return retVal;
    }
}
